<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;

class Already extends Controller{
    public function __construct(){
        parent::__construct();
        if (!Session::has('admin')){
            $this->redirect('index/login');
        }
    }

    //查出个人表
    public function person(){
        if ((Session::get('apartment')!=='')){
            $preject=Db::table('preject')->where('apartment',Session::get('apartment'))->select();
            $this->assign('preject',$preject);
        }else{
            $preject=Db::table('preject')->select();
            $this->assign('preject',$preject);
        }

        if (request()->isPost()){
            if (input('search')!==''){
                    Session::set('search',input('search'));
                if(Session::get('apartment')!==''){
                    $person=Db::table('person')->where('apartment',Session::get('apartment'))->whereLike('name|apartment|stuid|grade|class','%'.input('search').'%')->paginate(30);
                    $this->assign('person',$person);
                    $this->redirect(substr($_SERVER["HTTP_REFERER"],0,strrpos($_SERVER["HTTP_REFERER"],strrchr($_SERVER["HTTP_REFERER"],"."))).".html?page=1");
                }else{
                    $person=Db::table('person')->whereLike('name|apartment|preject','%'.input('search').'%')->paginate(30);
                    $this->assign('person',$person);
                    $this->redirect(substr($_SERVER["HTTP_REFERER"],0,strrpos($_SERVER["HTTP_REFERER"],strrchr($_SERVER["HTTP_REFERER"],"."))).".html?page=1");
                }
            }else{
                    Session::delete('search');
                    $uid=input('post.uid');
                    Session::set('preject',array('uid'=>$uid));
                    $person=Db::table('person')->where('uid',$uid)->paginate(30);
                    $this->assign('person',$person);
                   if(is_numeric(Session::get('preject.uid'))){
                    $this->assign('uid',Session::get('preject.uid'));
                       $this->redirect(substr($_SERVER["HTTP_REFERER"],0,strrpos($_SERVER["HTTP_REFERER"],strrchr($_SERVER["HTTP_REFERER"],"."))).".html?page=1");
                  }
            }
         }

        if (request()->get()){
            if (Session::has('search')){
                if(Session::get('apartment')!==''){
                    $person=Db::table('person')->where('apartment',Session::get('apartment'))->whereLike('name|apartment|stuid|grade|class','%'.Session::get('search').'%')->paginate(30);
                    $this->assign('person',$person);
                }else{
                    $person=Db::table('person')->whereLike('name|apartment|preject','%'.Session::get('search').'%')->paginate(30);
                    $this->assign('person',$person);
                }
                $this->assign('person',$person);
            }else{
                $person=Db::table('person')->where('uid',Session::get('preject.uid'))->paginate(30);
                if(is_numeric(Session::get('preject.uid'))){
                    $this->assign('uid',Session::get('preject.uid'));
                }
                $this->assign('person',$person);
            }

        }
        return view();
    }
    //查出团队表
    public function team(){
        $preject=Db::table('preject')->select();
        $this->assign('preject',$preject);

        $team=Db::table('team')->paginate(30);
        $this->assign('team',$team);
        return view();
    }
    //导出个人表
    public function excelperson(){
       $data=Db::table('person')->where('uid',input('get.uid'))->select();
        $tableName  = $data[0]['preject']."报名表";
      
                  vendor("PHPExcel.PHPExcel");
    $objPHPExcel = new \PHPExcel();
    // 设置sheet
    $objPHPExcel->setActiveSheetIndex(0);
    // 设置列的宽度
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
          $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
          $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);
    // 设置表头
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', '项目');
    $objPHPExcel->getActiveSheet()->SetCellValue('B1', '姓名');
    $objPHPExcel->getActiveSheet()->SetCellValue('C1', '学号');
    $objPHPExcel->getActiveSheet()->SetCellValue('D1', '年级');
    $objPHPExcel->getActiveSheet()->SetCellValue('E1', '班级');
    $objPHPExcel->getActiveSheet()->SetCellValue('F1', '联系方式');
    $objPHPExcel->getActiveSheet()->SetCellValue('G1', '邮箱');
    $objPHPExcel->getActiveSheet()->SetCellValue('H1', '系部');
    //存取数据
    $num = 2;

    foreach ($data as $k => $v) {
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, $v['preject']);
        $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['name']);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['stuid']);
        $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['grade']);
        $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['class']);
         $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, $v['phone']);
         $objPHPExcel->getActiveSheet()->SetCellValue('G' . $num, $v['email']);
         $objPHPExcel->getActiveSheet()->SetCellValue('I' . $num, $v['apartment']);
        $num++;
    }


      
    // 文件名称
    $fileName =$tableName;
    $xlsName = iconv('utf-8', 'gb2312', $fileName);
    // 设置工作表名
    $objPHPExcel->getActiveSheet()->setTitle('sheet');
    //下载 excel5与excel2007
    $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
    ob_end_clean();     // 清除缓冲区,避免乱码
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");
    header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
    header("Content-Transfer-Encoding:binary");
    $objWriter->save("php://output");
      
     /*   $Cell  = array(
            array('preject','项目'),
            array('name','姓名'),
            array('stuid','学号'),
            array('grade','年级'),
            array('class','班级'),
            array('phone','联系方式'),
            array('email','邮箱'),
            array('apartment','系部')
        );
        $data=Db::table('person')->where('uid',input('get.uid'))->select();
        $tableName  = $data[0]['preject']."报名表";
        $this->PHPExcel($tableName,$Cell,$data);
   */
    }
    //导出团队表
    public function excelteam(){
        $tableName  = "团队表";
        $Cell  = array(
            array('preject','项目'),
            array('name','姓名'),
            array('stuid','学号'),
            array('grade','年级'),
            array('class','班级'),
            array('phone','联系方式'),
            array('email','邮箱'),
            array('apartment','系部')
        );
        $data=Db::table('team')->select();
        $this->PHPExcel($tableName,$Cell,$data);
    }
}